/**
 *
 */

package lohmar.ashar.android.batterySaver.helper;

import android.content.Context;
import android.content.ContextWrapper;
import android.net.wifi.WifiManager;

/**
 * @author Mihai HARAS (Lohmar ASHAR) 2011
 */
public class WifiHelper implements IServiceHelper {

    private ContextWrapper context;

    public WifiHelper(ContextWrapper context) {
        this.context = context;
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.android.batterySaver.helper.IServiceHelper#isServiceEnabled()
     */
    @Override
    public boolean isServiceEnabled() {
        return getWifiManager().isWifiEnabled();
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.android.batterySaver.helper.IServiceHelper#isServicePresent()
     */
    @Override
    public boolean isServicePresent() {
        return getWifiManager() != null;
    }

    /*
     * (non-Javadoc)
     * @see lohmar.ashar.android.batterySaver.helper.IServiceHelper#setServiceEnabled(boolean)
     */
    @Override
    public void setServiceEnabled(boolean serviceEnabled) {
        try {
            WifiManager wifiManager = getWifiManager();
            if (wifiManager.isWifiEnabled() != serviceEnabled) {
                wifiManager.setWifiEnabled(serviceEnabled);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private WifiManager getWifiManager() {
        return (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }

    @Override
    public String getServiceName() {
        return "wifi";
    }

}
